<?php
namespace app\wxapp\controller;

class Useseal extends Base{


	public function __construct(){
		parent::__construct();
		$this->use = model("useseal");
		//$this->audit = model("usesealaudit");
		$this->log = model("usesealLog");
		$this->option = model("Option");
	}




	public function getlist(){

		$this->_mustPost();

		$pageno = input("pageno");
		$pagesize = input("pagesize");

		$type_id = input("type_id"); // 1用章 2维护 
		$apply_user_id = input("apply_user_id"); 
		$status = input("status");

		$map=array();
		$map['type_id'] = $type_id;
		$map['apply_user_id'] = $apply_user_id;
		//888全部
		if( $status != 888 ){	$map['status'] = $status; }

		$res = $this->use->where($map)->order('id desc')->page( $pageno , $pagesize)->select();
// $this->res['sql'] = db()->getLastSql();
		foreach($res as $k => $v){
			$res[$k]['status_text'] = $v->status_text;
			$v->seal;
			$v->equipment;
			
		}

		if($res){
			$this->res["data"] = $res;
		}else{
			$this->res["code"] = 999;
			$this->res["msg"] = '没有数据';
			$this->res["data"] = [];
		}


		die(myJSON($this->res));
	}



	public function getprocess(){
		$this->_mustPost();
		$type_id = input("type_id/d", 1);
		$user_id = input("user_id/d", 0);

		if(!$type_id || !$user_id){
			$this->res["code"] = 1001;
			$this->res["msg"] = '参数错误';
		}

		$map= array();
        $map['a.type_id'] = $type_id;//用章
        $map['c.user_id'] = $user_id;

        $res = model('process')->alias('a')
                           ->distinct(true)
                           ->field('a.*')
                           ->join('__PROCESS_APPLY__ b ','b.id= a.id')
                           ->join('__USER_ROLE__ c ','c.role_id= b.role_id')
                           ->where($map)
                           ->select();
        if($res){
			$this->res["data"] = $res;
		}else{
			$this->res["code"] = 999;
			$this->res["msg"] = '没有数据';
			$this->res["data"] = [];
		}


		die(myJSON($this->res));


	}

	public function getedit(){
		$id = input('id/d',0);
		$process_id = input('process_id/d',0);
		$user_id = input('user_id/d',0);

		if( !$user_id  ){
			$this->res["code"] = 1501;
			$this->res["msg"] = '参数错误';
			die(myJSON($this->res));
		}

		if( !$id &&  !$process_id  ){
			$this->res["code"] = 1502;
			$this->res["msg"] = '参数错误';
			die(myJSON($this->res));
		}

		if( $id ){
			//有$id 是修改
			$rs = model('useseal')->where('id' , $id ) -> find();
			$d= array();

			$d['id'] = $id;
			$d['process_id'] = $rs->process_id;
			$d['process_name'] = $rs->process->name;
			$d['equipment_name'] = $rs->equipment->equipment_name;
			$d['equipment_address'] = $rs->equipment->equipment_address;
			$d['seal_name'] = $rs->seal->seal_name;
			$d['max_times'] = $rs['max_times'];
			$d['exp_time_day'] = $rs['exp_time_day'];
			$d['info'] = $rs['info'];
			$d['attachs'] = array();
		}else{
			//没有$id 是添加
			$rs = model('process')->where('id' , $process_id) -> find();
			$d= array();

			$d['id'] = $id;
			$d['process_id'] = $process_id;
			$d['process_name'] = $rs->name;
			$d['equipment_name'] = $rs->equipment->equipment_name;
			$d['equipment_address'] = $rs->equipment->equipment_address;
			$d['seal_name'] = $rs->seal->seal_name;
			$d['max_times'] = 1;
			$d['exp_time_day'] = 7;
			$d['info'] = '';
			$d['attachs'] = array();


		}
// echo $rs;
		$this->res['data'] = $d;
		die(myJSON($this->res));

	}


	public function whgetedit(){
		$id = input('id/d',0);
		$process_id = input('process_id/d',0);
		$user_id = input('user_id/d',0);

		if( !$user_id  ){
			$this->res["code"] = 1501;
			$this->res["msg"] = '参数错误';
			die(myJSON($this->res));
		}

		if( !$id &&  !$process_id  ){
			$this->res["code"] = 1502;
			$this->res["msg"] = '参数错误';
			die(myJSON($this->res));
		}

		if( $id ){
			//有$id 是修改
			$rs = model('useseal')->where('id' , $id ) -> find();
			$d= array();

			$d['id'] = $id;
			$d['process_id'] = $rs->process_id;
			$d['process_name'] = $rs->process->name;
			$d['equipment_name'] = $rs->equipment->equipment_name;
			$d['equipment_address'] = $rs->equipment->equipment_address;
			$d['max_times'] = $rs['max_times'];
			$d['exp_time_day'] = $rs['exp_time_day'];
			$d['info'] = $rs['info'];
			// $d['attachs'] = array();
		}else{
			//没有$id 是添加
			$rs = model('process')->where('id' , $process_id) -> find();
			$d= array();

			$d['id'] = $id;
			$d['process_id'] = $process_id;
			$d['process_name'] = $rs->name;
			$d['equipment_name'] = $rs->equipment->equipment_name;
			$d['equipment_address'] = $rs->equipment->equipment_address;
			$d['max_times'] = 1;
			$d['exp_time_day'] = 7;
			$d['info'] = '';
			// $d['attachs'] = array();


		}
// echo $rs;
		$this->res['data'] = $d;
		die(myJSON($this->res));

	}

	//图片上次
	public function uploadimg(){

		action('Admin/File/upload');
		die();

	}


	public function upload(){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('file');
	    
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	            // 成功上传后 获取上传信息
	            // 输出 jpg
	            echo $info->getExtension();
	            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
	            echo $info->getSaveName();
	            // 输出 42a79759f284b767dfcb2a0197904287.jpg
	            echo $info->getFilename(); 
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
	    }
	}

	//用章申请保存
	public function editsave(){
		$this->_mustPost();



		$data = array(
            'id'            =>input('post.id/d',0),
            'process_id'    =>input('post.process_id/d',0),
            'status'        =>input('post.status/d',0), // 0 1 
            'max_times'     =>input('post.max_times/d',0),
            'info'          =>input('post.info/s',0),
            'attach'        =>implode( ',' , input('post.attachs/a',[])),
            'exp_time_day'      =>input('post.exp_time_day/d',1),
        );
        $process = model('Process')->find($data['process_id']);         
        $data['type_id'] = $process['type_id'];
        $data['equipment_id'] = $process['equipment_id'];
        $data['seal_id'] = $process['seal_id'];

        $data['exp_time'] = date("Y-m-d",strtotime("+".($data['exp_time_day']-1)." day"));

        $data['apply_user_id'] = input('post.user_id/d',1);





        $model = model('Useseal');
        if ($data) {

            $result = $model
                ->isUpdate( !!$data['id'] )
                ->validate('useseal.add')
                // ->allowField($allowField )
                ->save($data);
            if (false === $result) {
                return $this->error( $model->getError() );
            } else {

                //更新附件关联表
                $model->attachs()->detach(); //删除中间联表数据
                $model->attachs()->saveAll(input('post.attachs/a',[]));//增加

                //如果 状态 提交 创建审核数据
                if( $data['status'] == 1 ){
                    $model->makeAuditData( $model->process_id );
                }

                
                if($data['status'] == 0 ){
                	$this->res['msg'] = '保存成功';
					die(myJSON($this->res));
                }else if( $data['status'] == 1  ){
                    $this->res['msg'] = '保存并提交申请';
					die(myJSON($this->res));

                }

                $this->res['msg'] = '提交？？？';
				die(myJSON($this->res));
            }
        } else {
            $this->res['msg'] = '没有提交数据';
			die(myJSON($this->res));
        }  
	}

	//读取信息
	public function getinfo(){

		$id = input('id/d',0);
		$user_id = input('user_id/d',0);

		if(!$id || !$user_id ){
			$this->res['code'] = '999';
			$this->res['msg'] = '参数错误';
			die(myJSON($this->res));
		}
		$rs  = model('useseal')->find($id);
		// if( $rs && $rs['apply_user_id'] != $user_id ){
		// 	$this->res['code'] = '999';
		// 	$this->res['msg'] = '无权查看';
		// 	die(myJSON($this->res));
		// }
		$rs['status_text'] = $rs['status_text'];
		$rs->process;
		$rs->equipment;
		$rs->user;
		$rs->attachs;
		$rs->auds = $rs->getAudits();
		foreach( $rs->auds as $k => $v ){
			foreach( $v as $kk => $vv ){
				$vv->user;
				$vv->flag_text = $vv->flag_text ;
			};
		}

		//判断是 审核人，并且有审核 状态
        $audit_user = model('useseal_audit')->where(['id' => $id , 'user_id' => $user_id , 'status' => 0 ])->find();


        $rs->audit_user = $audit_user;

// echo $rs;
		$this->res['data'] = $rs;

		die(myJSON($this->res));

	}



	/**
     * 审核信息 提交保存
     * @return [type] [description]
     */
    public function auditSave(){
        $data = input('post.');
        if($data['flag'] == ''){
			$this->res['code'] = '999';
			$this->res['msg'] = '请批示[同意]or[驳回]';
			die(myJSON($this->res));

        }
        if($data['flag'] == 2 && empty( $data['mark']) ){
            $this->res['code'] = '999';
			$this->res['msg'] = '审核不通过，必须在批示中填写原因！';
			die(myJSON($this->res));
        }

        $model =  model('useseal_audit');

        $rs =   $model
        ->isUpdate(true)
        ->allowField('flag,mark, id , step ,  user_id ')
        ->save($data);

        if($rs === false){
            $this->res['code'] = '999';
			$this->res['msg'] = $model->getError().'审核不通过，必须在批示中填写原因！';
			die(myJSON($this->res));

        }else{
            model('useseal')->checkAudit($data['id']);

			die(myJSON($this->res));

        };

    }
}